/*
 * @Descripttion:
 * @version:
 * @Author: ZhangXue
 * @Date: 2021-07-12 09:17:30
 * @LastEditors: ZhangXue
 * @LastEditTime: 2021-07-29 14:45:20
 */
import Vue from "vue";
import Router from "vue-router";

/** * 大数据系统登录路由 开始***/
const login = () => import("@/pages/login/login");

/* 首页 */
const home = () => import("@/pages/home");

/** * 大数据系统路由 开始***/
// 首页
const index = () => import("@/pages/index/index");
// APP统计页面
const appStatistics = () => import("@/pages/index/appStatistics");
// APP统计页面
const electricStatistics = () => import("@/pages/index/electricStatistics.vue");

// 地域分布页面
const areaDistribution = () => import("@/pages/areaDistribution/areaDistribution");

// 智慧消防分析系统
const firePage = () => import("@/pages/firePage");

// 接口调用工具
const interfaceToll = () => import("@/pages/interfaceToll");
/** 404页面 **/
const notFound = () => import("@/pages/notFound");

Vue.use(Router);

// 避免点击重复路由时报错
const originalPush = Router.prototype.push;

// eslint-disable-next-line space-before-function-paren
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const routers = new Router({
  mode: "history",
  routes: [{
    path: "/",
    redirect: "/home"
  }, {
    path: "/home",
    name: "home",
    component: home,
    meta: {
      title: "大雪呀"
    }
  }, {
    path: "/login",
    name: "login",
    component: login
  }, {
    path: "/index",
    name: "index",
    component: index,
    redirect: "/index/appStatistics",
    children: [{
      path: "appStatistics",
      name: "appStatistics",
      component: appStatistics,
      meta: {
        title: "大数据分析系统"
      }
    }, {
      path: "electricStatistics",
      name: "electricStatistics",
      component: electricStatistics,
      meta: {
        title: "大数据分析系统"
      }
    }]
  }, {
    path: "/areaDistribution",
    name: "areaDistribution",
    component: areaDistribution,
    meta: {
      title: "新冠疫情地图"
    }
  }, {
    path: "/firePage",
    name: "firePage",
    component: firePage,
    meta: {
      title: "智慧消防视频分析系统"
    }
  }, {
    path: "/interfaceToll",
    name: "interfaceToll",
    component: interfaceToll,
    meta: {
      title: "接口调用工具"
    }
  }, {
    path: "/404",
    name: "NotFound",
    component: notFound,
    meta: {
      title: "404页面不存在"
    }
  },
  {
    path: "*",
    name: "NotFound",
    component: notFound,
    meta: {
      title: "404页面不存在"
    }
  }
  ]
});

// 访问路由前，先判断是否有访问的权限，没有的话跳转到404
routers.beforeEach((to, from, next) => {
  // const rights = window.sessionStorage.getItem("username") ? window.sessionStorage.getItem("username") : "";
  if (to.path === "/login" || to.path === "/404") {
    next();
    return;
  }
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  // if (to.meta.requireAuth) {
  // if (rights) {
  //   next();
  // } else {
  //   // console.log("暂无相关权限！");
  //   next("/login");
  // }
  next();
});

export default routers;
